package com.lsa.southwind.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsa.southwind.entity.DormitoryAdmin;
import com.lsa.southwind.form.RuleForm;
import com.lsa.southwind.form.SearchForm;
import com.lsa.southwind.mapper.DormitoryAdminMapper;
import com.lsa.southwind.service.DormitoryAdminService;
import com.lsa.southwind.vo.PageVo;
import com.lsa.southwind.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-06-01
 */
@Service
public class DormitoryAdminServiceImpl extends ServiceImpl<DormitoryAdminMapper, DormitoryAdmin> implements DormitoryAdminService {
    @Autowired
    private DormitoryAdminMapper dormitoryAdminMapper;

    @Override
    public ResultVo login(RuleForm ruleForm) {
        //1.判断用户名是否存在
        QueryWrapper<DormitoryAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", ruleForm.getUsername());
        DormitoryAdmin dormitoryAdmin = this.dormitoryAdminMapper.selectOne(queryWrapper);
        ResultVo resultVo = new ResultVo();
        if (dormitoryAdmin == null) {
            resultVo.setCode(-1);
        } else {
            //2.判断用户名是否存在
            if (!dormitoryAdmin.getPassword().equals(ruleForm.getPassword())) {
                resultVo.setCode(-2);
            } else {
                resultVo.setCode(0);
                resultVo.setData(dormitoryAdmin);
            }

        }

        return resultVo;
    }

    @Override
    public PageVo list(Integer page, Integer size) {
        /*分页查询 page页数,size大小*/
        Page<DormitoryAdmin> dormitoryAdminPage = new Page<>(page, size);
        /*
        selectPage：根据 entity 条件，查询全部记录
        selectMapsPage：根据 Wrapper 条件，查询全部记录
        */
        Page<DormitoryAdmin> resultPage = this.dormitoryAdminMapper.selectPage(dormitoryAdminPage, null);
        PageVo pageVo = new PageVo();
        /*获取总条数*/
        pageVo.setTotal(resultPage.getTotal());
        pageVo.setData(resultPage.getRecords());
        return pageVo;
    }

    @Override
    public PageVo search(SearchForm searchForm) {
        //模糊查询+分页、
        Page<DormitoryAdmin> dormitoryAdminPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<DormitoryAdmin> resultPage = null;
        if (searchForm.getValue().equals("")) {
            /*如果查询为空就是查询全部*/
            resultPage = this.dormitoryAdminMapper.selectPage(dormitoryAdminPage, null);
        } else {

            QueryWrapper<DormitoryAdmin> queryWrapper = new QueryWrapper<>();
            /*字段名,值*/
            queryWrapper.like(searchForm.getKey(), searchForm.getValue());
            resultPage = this.dormitoryAdminMapper.selectPage(dormitoryAdminPage, queryWrapper);
        }
        PageVo pageVo = new PageVo();
        /*查询出来的条数*/
        pageVo.setTotal(resultPage.getTotal());
        /*查询出来的数据*/
        pageVo.setData(resultPage.getRecords());
        return pageVo;
    }

}
